package com.wnxy.carl.commons;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

public class BasicFormatUtil {
    public static final String BYTE = "java.lang.Byte";
    public static final String CHARACTER = "java.lang.Character";
    public static final String SHORT = "java.lang.Short";
    public static final String INTEGER = "java.lang.Integer";
    public static final String LONG = "java.lang.Long";
    public static final String FLOUT = "java.lang.Flout";
    public static final String DOUBLE = "java.lang.Double";
    public static final String BOOLEAN = "java.lang.Boolean";
    public static final String Bbyte = "byte";
    public static final String Bchar = "char";
    public static final String Bshort = "short";
    public static final String Bint = "int";
    public static final String Blong = "long";
    public static final String Bflout = "flout";
    public static final String Bdouble = "double";
    public static final String Bboolean = "boolean";

    public static <T> T parseMap(Map<Object,Object> maps,Class<T> type){
        T t = null;
        try {
          t = type.getDeclaredConstructor().newInstance();
          for (Object key : maps.keySet()){
              String s = key.toString();
              Object value = maps.get(key);
              String setName = "set"+s.substring(0,1).toLowerCase()+s.substring(1);
              Class<?> fieldType = type.getDeclaredField(s).getType();
              Method declaredMethod = type.getDeclaredMethod(setName, fieldType);
              Object typeValue = typeValue(fieldType, value);
              declaredMethod.invoke(t,typeValue);
          }
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return t;
    }

    private static Object typeValue(Class<?> fieldType,Object value){
        Object typeV = null;
        String type = fieldType.getTypeName();
        if (BYTE.equals(type) || Bbyte.equals(type)){
            typeV = Byte.parseByte(value.toString());
        }else if (CHARACTER.equals(type) || Bchar.equals(type)){
            typeV = value.toString().charAt(0);
        }else if (SHORT.equals(type) || Bshort.equals(type)){
            typeV = Short.parseShort(value.toString());
        }else if (INTEGER.equals(type) || Bint.equals(type)){
            typeV = Integer.parseInt(value.toString());
        }else if (LONG.equals(type) || Blong.equals(type)){
            typeV = Long.parseLong(value.toString());
        }else if (FLOUT.equals(type) || Bflout.equals(type)){
            typeV = Float.parseFloat(value.toString());
        }else if (DOUBLE.equals(type) || Bdouble.equals(type)){
            typeV = Double.parseDouble(value.toString());
        }else if (BOOLEAN.equals(type) || Bboolean.equals(type)){
            typeV = Boolean.parseBoolean(value.toString());
        }
        return typeV;
    }
}
